package demo.spring.restful.controllers;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import ch.qos.logback.classic.Logger;
import demo.spring.restful.domain.Computer;
import demo.spring.restful.service.ComputerService;

@RestController
public class ComputerController {

	Logger logger = (Logger) LoggerFactory.getLogger(ComputerController.class);
	@Autowired
	private ComputerService computerService; // Service which will do all data
		
	// retrieval/manipulation work

	// -------------------Retrieve All
	// computers--------------------------------------------------------

	@RequestMapping(value = "/computer/", method = RequestMethod.GET)
	public ResponseEntity<List<Computer>> listAllcomputers() {
		List<Computer> computers = computerService.findAllComputers();
		if (computers.isEmpty()) {
			return new ResponseEntity<List<Computer>>(HttpStatus.NO_CONTENT);
		}
		logger.info("GET SUCCESS!");
		return new ResponseEntity<List<Computer>>(computers, HttpStatus.OK);
	}

	// -------------------Retrieve Single
	// Computer--------------------------------------------------------

	@RequestMapping(value = "/computer/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Computer> getComputer(@PathVariable("id") long id) {
		System.out.println("Fetching computer with id " + id);
		Computer Computer = computerService.findById(id);
		if (Computer == null) {
			System.out.println("computer with id " + id + " not found");
			logger.info("CANNOT FOUND!");
			return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
		}
		logger.info("GET ONE SUCCESS!");
		return new ResponseEntity<Computer>(Computer, HttpStatus.OK);
	}

	// -------------------Create a
	// Computer--------------------------------------------------------

	@RequestMapping(value = "/computer/", method = RequestMethod.POST)
	public ResponseEntity<Void> createComputer(@RequestBody Computer computer,
			UriComponentsBuilder ucBuilder) {
		System.out.println("Creating computer: " + computer.getBrand() + "  "
				+ computer.getVersion());

		if (computerService.isComputerExist(computer)) {
			System.out.println("A computer with brand " + computer.getBrand()
					+ ",version " + computer.getVersion()
					+ " already exist");
			logger.info("VERSION ALREADY EXIST!");
			return new ResponseEntity<Void>(HttpStatus.CONFLICT);
		}

		computerService.saveComputer(computer);

		HttpHeaders headers = new HttpHeaders();
		headers.setLocation(ucBuilder.path("/computer/{id}")
				.buildAndExpand(computer.getId()).toUri());
		logger.info("ADDING SUCCESS!");
		return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
	}

	// ------------------- Update a Computer
	// --------------------------------------------------------

	@RequestMapping(value = "/computer/{id}", method = RequestMethod.PUT)
	public ResponseEntity<Computer> updateComputer(@PathVariable("id") long id,
			@RequestBody Computer computer) {
		System.out.println("Updating computer " + id);

		Computer currentComputer = computerService.findById(id);

		if (currentComputer == null) {
			System.out.println("computer with id " + id + " not found");
			logger.info("CANNOT FOUNT WITH ID!");
			return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
		}

		currentComputer.setBrand(computer.getBrand());
		currentComputer.setVersion(computer.getVersion());
		currentComputer.setRam(computer.getRam());
		currentComputer.setScreen(computer.getScreen());

		computerService.updateComputer(currentComputer);
		logger.info("UPDATE WITH ID SUCCESS!");
		return new ResponseEntity<Computer>(currentComputer, HttpStatus.OK);
	}

	// ------------------- Delete a Computer
	// --------------------------------------------------------

	@RequestMapping(value = "/computer/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<Computer> deleteComputer(@PathVariable("id") long id) {
		System.out.println("Fetching & Deleting Computer with id " + id);

		Computer Computer = computerService.findById(id);
		if (Computer == null) {
			System.out.println("Unable to delete. computer with id " + id
					+ " not found");
			logger.info("CANNOT FOUND WITH ID!");
			return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
		}

		computerService.deleteComputerById(id);
		logger.info("DELETE WITH ID SUCCESS!");
		return new ResponseEntity<Computer>(HttpStatus.NO_CONTENT);
	}

	// ------------------- Delete All computers
	// --------------------------------------------------------

	@RequestMapping(value = "/computer/", method = RequestMethod.DELETE)
	public ResponseEntity<Computer> deleteAllcomputers() {
		System.out.println("Deleting All computers");

		computerService.deleteAllcomputers();
		logger.info("DELETE SUCCESS!");
		return new ResponseEntity<Computer>(HttpStatus.NO_CONTENT);
	}
}
